﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeProjectsModel;
using WeProjects.Filters;
using System.Web.Security;

namespace WeProjects.Controllers
{
    [Authorize( Roles="sysadmin")]
    [InitializeSimpleMembership]
    public class NoticesController : Controller
    {
        private ProjectsDBEntities db = new ProjectsDBEntities();

        //
        // GET: /News/
        [AllowAnonymous]
        public ActionResult Index()
        {
                return View(db.Notices.ToList());
        }
        //
        // GET: /News/Details/5
        [AllowAnonymous]
        public ActionResult Details(int id = 0)
        {
            Notice notice = db.Notices.Find(id);
            if (notice == null)
            {
                return HttpNotFound();
            }
            
            return View(notice);
        }

        //
        // GET: /News/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /News/Create
        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Create(string Title, string NewsContent)
        {
            var notice = new Notice();
            notice.Title = Title;
            notice.NoticeContent = NewsContent;
            notice.PublishDate = DateTime.Now;

            if (ModelState.IsValid)
            {
                db.Notices.Add(notice);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(notice);
        }
        //
        // GET: /News/Edit/5
        public ActionResult Edit(int id = 0)
        {
            Notice notice = db.Notices.Find(id);
            if (notice == null)
            {
                return HttpNotFound();
            }
            return View(notice);
        }

        //
        // POST: /News/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(string Title, string NewsContent,int ID)
        {
            var notice = new Notice();
            notice.ID = ID;
            notice.Title = Title;
            notice.NoticeContent = NewsContent;
            notice.PublishDate = DateTime.Now;

            if (ModelState.IsValid)
            {
                db.Entry(notice).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            //ViewBag.ProjectID = new SelectList(db.Projects, "ID", "Title", news.ProjectID);
            return View(notice);
        }

        //
        // GET: /News/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Notice notice = db.Notices.Find(id);
            if (notice == null)
            {
                return HttpNotFound();
            }
            return View(notice);
        }

        //
        // POST: /News/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Notice notice = db.Notices.Find(id);
            db.Notices.Remove(notice);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}